package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.example.game.ChessBoard;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.HashMap;

/**
 * @author: HanTing Wang
 * @description:
 * @version:
 * @date: 2022/1/1
 */
@Controller
public class ViewController {

    @GetMapping("/test")
    public String test(HttpSession session, Model model) {
        model.addAttribute("sessionID", session.getId());
        return "test";
    }

    @GetMapping("/multi")
    public String multi(HttpSession session, Model model) {
        model.addAttribute("clientID", session.getId());
        return "multi";
    }

    @GetMapping("/solo")
    public String solo(HttpSession session) {
        ChessBoard chessBoard = new ChessBoard();
        session.setAttribute("chessBoard", chessBoard);
        return "solo";
    }

    @GetMapping("/solo/drop/{color}/{x}/{y}") @ResponseBody
    public String drop(HttpSession session,
                       @PathVariable("x") int x,
                       @PathVariable("y") int y,
                       @PathVariable("color") int color) {
        try {
            ChessBoard chessBoard = (ChessBoard) session.getAttribute("chessBoard");
            boolean status = chessBoard.dropChess(color, x, y);
            if (status) {
                return JSON.toJSONString(new HashMap<String, Object>() {
                    {put("status", true);}
                });
            } else {
                return JSON.toJSONString(new HashMap<String, Object>() {
                    {put("status", false);}
                });
            }
        } catch (Exception ignored) {
        }
        return null;
    }

    @GetMapping("/solo/ai") @ResponseBody
    public String ai(HttpSession session){
        ChessBoard chessBoard = (ChessBoard) session.getAttribute("chessBoard");
        boolean status = chessBoard.AIDrop();
        return String.valueOf(status);
    }

}
